package com.bigdata.demo.web.controller;

import com.bigdata.demo.base.ApiResponse;
import com.bigdata.demo.service.CRReportService;
import com.bigdata.demo.service.ServiceMultiResult;
import com.bigdata.demo.service.ServiceResult;
import com.bigdata.demo.service.kg.KGService;
import com.bigdata.demo.web.dto.CADTO;
import com.bigdata.demo.web.dto.CRDTO;
import com.bigdata.demo.web.dto.IERDTO;
import com.bigdata.demo.web.form.CRReportSearch;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.util.List;


@Controller
public class KGController {


    @Autowired
    private CRReportService crReportService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private KGService kgService;

    @GetMapping("/kg/main")
    public String mainPage(){
        return "kg/main";
    }

    @GetMapping("/kg/crkg")
    public String crkgPage(){

        return "kg/crkg";
    }

    @GetMapping("/kg/crkgdetail")
    @ResponseBody
    public String crkgdetail( @ModelAttribute CRReportSearch  searchBody ){
        String jsonString=null;
        if(searchBody.getKks()==null)
            jsonString=kgService.searchByKKS("1LBQ13AA410");
        else
            jsonString=kgService.searchByKKS(searchBody.getKks());
        return jsonString;
    }

    @GetMapping("view/cr")
    public String crInfo(@ModelAttribute CRReportSearch  searchBody, Model model) {
        ServiceResult<CRDTO> result=null;
        if(searchBody.getId_key_cr()!=null){
            result     = crReportService.getCRInfo(searchBody.getId_key_cr());
        }
        CRDTO crdto=result.getResult();
        model.addAttribute("cr",crdto);
        return "kg/crinfo";
    }
    @GetMapping("view/ca")
    public String caInfo(@ModelAttribute CRReportSearch  searchBody, Model model) {
        ServiceResult<CADTO> result=null;
        if(searchBody.getCano()!=null){
            result     = crReportService.getCAInfo(searchBody.getCano());
        }
        CADTO cadto=result.getResult();
        model.addAttribute("ca",cadto);
        return "kg/cainfo";
    }
    @GetMapping("view/kks")
    public String kksInfo(@ModelAttribute CRReportSearch  searchBody, Model model) {
        ServiceMultiResult<CRDTO> result=null;
        if(searchBody.getCr_equipment()!=null){
            result   = crReportService.getKKSCrs(searchBody.getCr_equipment());
        }
        List<CRDTO> list=result.getResult();
        model.addAttribute("list",list);
        return "kg/kksinfo";
    }

    @GetMapping("view/ier")
    public String ierInfo(@ModelAttribute CRReportSearch  searchBody, Model model) {
        ServiceResult<IERDTO> result=null;
        if(searchBody.getErno()!=null){
            result     = crReportService.getIERInfo(searchBody.getErno());
        }
        IERDTO ierdto=result.getResult();
        model.addAttribute("ier",ierdto);
        return "kg/ierinfo";
    }


}
